package com.isdepci.project.factor.controller;

import java.util.List;

import com.isdepci.common.utils.StringUtils;
import com.isdepci.project.factor.domain.EnterpriseInfo;
import com.isdepci.project.factor.utils.FactorRedisUtils;
import com.isdepci.project.factor.utils.FactorUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.isdepci.framework.aspectj.lang.annotation.Log;
import com.isdepci.framework.aspectj.lang.enums.BusinessType;
import com.isdepci.project.factor.domain.ProductCategorize;
import com.isdepci.project.factor.service.IProductCategorizeService;
import com.isdepci.framework.web.controller.BaseController;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.common.utils.poi.ExcelUtil;
import com.isdepci.framework.web.page.TableDataInfo;

/**
 * 产品分类Controller
 *
 * @author isdepci
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/factor/productCategorize")
public class ProductCategorizeController extends BaseController {

    @Autowired
    private IProductCategorizeService productCategorizeService;
    @Autowired
    private FactorUtils factorUtils;

    @GetMapping("/dict")
    public AjaxResult selectProductCategorizeDict() {
        List<ProductCategorize> list = factorUtils.selectProductCategorizeDict();
        return AjaxResult.success(list);
    }

    /**
     * 查询产品分类列表
     */
    @PreAuthorize("@ss.hasPermi('factor:productCategorize:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductCategorize productCategorize) {
        startPage();
        List<ProductCategorize> list = productCategorizeService.selectProductCategorizeList(productCategorize);
        return getDataTable(list);
    }


    /**
     * 获取产品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('factor:productCategorize:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productCategorizeService.selectProductCategorizeById(id));
    }

    /**
     * 新增产品分类
     */
    @PreAuthorize("@ss.hasPermi('factor:productCategorize:add')")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductCategorize productCategorize) {
        return (productCategorizeService.insertProductCategorize(productCategorize));
    }

    /**
     * 修改产品分类
     */
    @PreAuthorize("@ss.hasPermi('factor:productCategorize:edit')")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductCategorize productCategorize) {
        if (StringUtils.isNotEmpty(productCategorize.getParentCode())&&
                StringUtils.isNotEmpty(productCategorize.getCode())&&
                productCategorize.getParentCode().equals(productCategorize.getCode())) {
            return AjaxResult.error("修改'" + productCategorize.getChineseName() + "'失败，上级分类不能是自己");
        }
        return (productCategorizeService.updateProductCategorize(productCategorize));
    }

    /**
     * 删除产品分类
     */
    @PreAuthorize("@ss.hasPermi('factor:productCategorize:remove')")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return  (productCategorizeService.deleteProductCategorizeById(id));
    }


    @Log(title = "产品分类", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public AjaxResult clearCache() {
        FactorRedisUtils.clearProductCategorizeListCache();
        return AjaxResult.success();
    }
}
